package org.jeecg.modules.feeElectricity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.feeElectricity.entity.FeeElectricity;
import org.jeecg.modules.feeElectricity.entity.FeeElectricitySelectDTO;
import org.jeecg.modules.feeElectricity.mapper.FeeElectricityMapper;
import org.jeecg.modules.feeElectricity.service.IFeeElectricityService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 电费
 * @Author: jeecg-boot
 * @Date:   2023-07-06
 * @Version: V1.0
 */
@Service
public class FeeElectricityServiceImpl extends ServiceImpl<FeeElectricityMapper, FeeElectricity> implements IFeeElectricityService {

    @Override
    public List<FeeElectricity> selectByTimeAndDept(FeeElectricitySelectDTO selectDTO) {
        Date start = Date.from(selectDTO.getStartTime().toInstant());
        Date end = Date.from(selectDTO.getEndTime().toInstant());
        String paramS = DateUtils.formatDate(start, "yyyyMMddHH");
        String paramE = DateUtils.formatDate(end, "yyyyMMddHH");
        int s = Integer.parseInt(paramS);
        int e = Integer.parseInt(paramE);
        if (selectDTO.getStartTime() == null || selectDTO.getEndTime() == null) {
            throw new JeecgBootException("服务器忙,请稍后再试...");
        }
        QueryWrapper<FeeElectricity> wrapper = new QueryWrapper<>();
        if ("date".equals(selectDTO.getDatePickerType())) {
            wrapper.select("sum(fee) as fee", "year", "month", "day").between("date", s, e)
                    .eq("dept", selectDTO.getDept())
                    .groupBy("month", "year", "day").orderByAsc("year*1", "month*1", "day*1");
        } else if ("datetime".equals(selectDTO.getDatePickerType())) {
            wrapper.between("date", s, e).eq("dept", selectDTO.getDept()).orderByAsc("date");
        } else {
            wrapper.select("sum(fee) as fee", "year", "month").between("date", s, e)
                    .eq("dept", selectDTO.getDept())
                    .groupBy("month", "year").orderByAsc("year*1", "month*1");
        }
        return baseMapper.selectList(wrapper);
    }
}
